package org.hyh.core.security.handler;

import org.hyh.core.Exception.TokenAuthenticationException;
import org.hyh.core.model.vo.ResultVo;
import org.hyh.core.utils.ResponseUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @auther :hyh
 * @desc :
 * @date :2020/7/12
 */

/**
 *   这个主要是exceptionFilter 处理异常的类。
 *   常用于认证失败等情况。
 */


public class handlerWhenNoAuth  implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String msg = "权限不足或登录超时";
        if (authException instanceof TokenAuthenticationException) {
              msg=  authException.getMessage();
        }
        ResultVo resultVo = ResultVo.buildFail(msg);
        ResponseUtils.resetAndResponseToClient(response,resultVo,true);
    }
}
